package cn.psvmc.cxzapi.service.customer;

import cn.psvmc.cxzapi.config.mybatisplus.service.impl.AbstractService;
import cn.psvmc.cxzapi.exception.ServiceException;
import cn.psvmc.cxzapi.mapper.customer.TCustomerBuyoutMapper;
import cn.psvmc.cxzapi.model.customer.TCustomerBuyout;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 客户买断申请表 服务实现类
 * @Author auto
 * @Date 2024-08-12
 */
@Service
public class TCustomerBuyoutService extends AbstractService<TCustomerBuyoutMapper, TCustomerBuyout> {

    @Resource
    private TCustomerBuyoutMapper tCustomerBuyoutMapper;

    /**
    * @Date auto 2024-08-12 新增客户买断申请表
    * @Param tCustomBuyout 客户买断申请表
    * @Return int
    */
    public int add(TCustomerBuyout tCustomerBuyout){
        return tCustomerBuyoutMapper.insert(tCustomerBuyout);
    }

    /**
    * @Date auto 2024-08-12 更新客户买断申请表
    * @Param tCustomBuyout 客户买断申请表模型
    * @Return int
    */
    public int edit(TCustomerBuyout tCustomerBuyout){
        // 通过id查询数据处理 详情
        TCustomerBuyout originTCustomerBuyout = this.detail(tCustomerBuyout.getCbid());
        if(originTCustomerBuyout == null){
            throw new ServiceException("未查询到对应的客户买断申请表信息！");
        }

        // 比较数据版本号
        if(!originTCustomerBuyout.getRversion().equals(tCustomerBuyout.getRversion())){
            throw new ServiceException("数据已更新，请刷新后重试！");
        }

        // 字段为null 不更新
        return tCustomerBuyoutMapper.updateById(tCustomerBuyout);
    }

    /**
    * @Date auto 2024-08-12 查询客户买断申请表详情
    * @Param id 唯一id
    * @Return cn.psvmc.cxzapi.model.custom.TCustomBuyout
    */
    public TCustomerBuyout detail(Integer id){
        return tCustomerBuyoutMapper.selectById(id);
    }

    /**
    * @Date auto 2024-08-12 查询客户买断申请表列表
    * @Param iPage 分页
    * @Return com.baomidou.mybatisplus.core.metadata.IPage<cn.psvmc.cxzapi.model.custom.TCustomBuyout>
    */
    public IPage<TCustomerBuyout> list(Page<TCustomerBuyout> iPage){
        return this.page(iPage);
    }
}
